export default {
	actions: {
		
	},
	mutations: {
		ADD_CART_LIST(state, cartItem) {
			let isExit = state.cartList.findIndex((item) => {
				return item.goodsDetail.id == cartItem.goodsDetail.id
			})
			console.log((isExit))
			if(isExit !== -1) {
				state.cartList.splice(isExit, 1)
			}
			state.cartList.unshift(cartItem)
			console.log((state.cartList))
		},
		CHANGE_CHECKED(state, index) {
			console.log(index, state.cartList)
			state.cartList[index].ischecked = !state.cartList[index].ischecked
			state.cartList.splice(1,0)
		},
		CHANGE_ALL_CHECKED(state, val) {
			state.cartList.forEach(item => {
				item.ischecked = val
			})
		},
		CHANGE_COUNT(state, obj) {
			state.cartList[obj.index].count = obj.value
		},
		CHANGE_SIZE(state, obj) {
			state.cartList[obj.index].currentIndex = obj.value
		}
	},
	state: {
		cartList: [{
			count: 1,
			currentIndex: 0,
			goodsDetail: {
				bcid: 1,
				bcname: "蛋糕",
				chineseBrief: "精选蓝莓的清爽可口/芝士的香浓/优质奶油的醇厚\r\n起酥皮的香脆可口/内层轻乳芝士的松软\r\n层层美味/回味无穷",
				createdAt: "2022-02-09T06:25:29.777Z",
				fid: null,
				fittings: "标配餐具(免费)5份",
				fname: null,
				french: "Napoléon aux myrtilles",
				id: "13705",
				img: "https://static.mcake.com/new_goods/lanmeiqingrunapolun/N0301/list/1.jpg",
				limg: "https://static.mcake.com/withwheat/list/R1334.jpg",
				list: [{
					ahead: "提前5小时预定",
					bcid: 1,
					bcname: "蛋糕",
					edible: "",
					fittings: "标配餐具(免费)5份",
					id: 13705,
					pic:{
						list: [
							{b: '/big/1.jpg', m: '/middle/1.jpg', s: '/small/1.jpg'},
							{b: '/big/2.jpg', m: '/middle/2.jpg', s: '/small/2.jpg'},
							{b: '/big/3.jpg', m: '/middle/3.jpg', s: '/small/3.jpg'},
						],
						url: "https://static.mcake.com/goods/lanmeiqingrunapolun/N0301"
					},
					pprice: "138.00",
					price: "138.00",
					size: "",
					sku: "R1334",
					spec: "0.5磅",
					weight: "227g",
					name: "Mini款-蓝莓轻乳拿破仑",
					objectId: "62035e59f61d191dff98970b",
					pprice: "138.00",
					price: "138.00",
					saleTotal: 1853,
					sid: null,
					sku: "R1334",
					sname: null,
					spec: "0.5磅",
					tid: 0,
					timg: "",
					tname: "",
					twoId: 13685,
					updatedAt: "2022-02-09T06:25:29.777Z",
					weight: "227g"
				}]
			},
			ischecked: true
		}]
	},
	getters: {
		totalPrice(state) {
			return state.cartList.reduce((pre, cur) => {
				let currentPrice = cur.ischecked ? cur.goodsDetail.list[cur.currentIndex].price * cur.count : 0
				return pre + currentPrice
			}, 0)
		},
		allChecked(state) {
			if(!state.cartList.length) return false
			return state.cartList.every(item => item.ischecked)
		}
	}
}